package com.budwk.app.sqfwV2.generator.dayingRoadPdf.rw;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.dayingRoad.DaYingLuWhiteRecordPdfDTO;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.deepoove.poi.data.PictureRenderData;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * PDF丧事办理酒席备案表.docx
 */
public class WhiteHandleCateringRecordsPdfGenerator extends BasePdfGenerator {
    @Override
    protected boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        DaYingLuWhiteRecordPdfDTO whiteRecordPdfDTO = (DaYingLuWhiteRecordPdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        // 政治面貌
        Boolean isPartyMemberRecordKeeper = whiteRecordPdfDTO.getIsPartyMemberRecordKeeper();
        paramMap.put("isPartyMemberRecordKeeper", isPartyMemberRecordKeeper ? "党员" : "其他");

        // 是否有公职人员
        Boolean familyHasPublicServantRecordKeeper = whiteRecordPdfDTO.getFamilyHasPublicServantRecordKeeper();
        paramMap.put("familyHasPublicServantRecordKeeper", familyHasPublicServantRecordKeeper ? "是" : "否");

        // 安葬时间
        Date burialTime = whiteRecordPdfDTO.getBurialTime();
        paramMap.put("yyyy", DateUtil.year(burialTime));
        paramMap.put("mm", DateUtil.month(burialTime) + 1);//月份从0开始,所以要加1
        paramMap.put("dd", DateUtil.dayOfMonth(burialTime));
        paramMap.put("hh", DateUtil.hour(burialTime, true));

        // 是否办酒
        if ("未办酒".equals(whiteRecordPdfDTO.getBanquetForm())) {
            paramMap.put("banquetCost", "-");
            paramMap.put("banquetTime", "-");
            paramMap.put("banquetLocation", "-");
            paramMap.put("banquetTableCount", "-");
        } else {
            // 办酒时间
            Date banquetTime = whiteRecordPdfDTO.getBanquetTime();
            paramMap.put("banquetTime", DateUtil.format(banquetTime, "yyyy-MM-dd"));

            // 桌餐标准
            Long banquetCost = whiteRecordPdfDTO.getBanquetCost();
            // 分转元
            paramMap.put("banquetCost", BigDecimal.valueOf(banquetCost).divide(new BigDecimal(100)));
        }

        // 火化时间
        Date deceasedCremationTime = whiteRecordPdfDTO.getDeceasedCremationTime();
        paramMap.put("deceasedCremationTime", DateUtil.format(deceasedCremationTime, "yyyy-MM-dd"));

        // 单份最高礼金数
        Long maximumAmount = whiteRecordPdfDTO.getMaximumAmount();
        // 分转元
        paramMap.put("maximumAmount", BigDecimal.valueOf(maximumAmount).divide(new BigDecimal(100)));

        // 执事费用
        Long attendantCost = whiteRecordPdfDTO.getAttendantCost();
        // 分转元
        paramMap.put("attendantCost", BigDecimal.valueOf(attendantCost).divide(new BigDecimal(100)));

        // 图片处理
        List<String> pictureUrlList = Arrays.asList(whiteRecordPdfDTO.getCheckedUnitLeader());
        String fieldName = "checkedUnitLeader";
        List<Map<String, PictureRenderData>> pictures = CommonFormOfficeUtils.addWordPicture(pictureUrlList, fieldName, 1.0F);
        paramMap.put(fieldName + "s", pictures);

        // 图片处理
        Object signature = pdfGenerateParam.getCustomerParamMap().get("signature");
        List<String> signatureList = Arrays.asList(signature == null ? "" : signature.toString());
        String signatureName = "signature";
        List<Map<String, PictureRenderData>> signatures = CommonFormOfficeUtils.addWordPicture(signatureList, signatureName, 1.0F);
        paramMap.put(signatureName + "s", signatures);
        if (signature != null && StringUtils.isNotBlank(signature.toString())) {
            paramMap.put("createUserName", "");
        }
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "daYingLu/white/handleCateringRecords.docx";
    }
}
